Ext.namespace("GridBlankInfo");
GridBlankInfo.Detail = function() {
	this.userSelector = null;
	this.selectUserId = null;
	this.users = [];
	
	this.txtBlankId = new Ext.form.Hidden({
		name:'BlankId'
	});
	//营销单元id
	this.txtUnitId = new Ext.form.Hidden({
		name:'UnitId',
		value:''
	});
	this.txtGridId=new Ext.form.Hidden({
		name:'gridId'
	});
	//空地名称
	this.txtBlankName = new Ext.form.TextField({
		fieldLabel : '空地名称<font color="red">*</font>',
		name :'BlankName',
		width : 380,
		maxLength : 100,
		allowBlank:false
	});
	//空地规划方向
	this.cmbDevelopDesc = new Ext.form.TextArea({
        width: 380,
        fieldLabel: '空地规划方向',
        maxLength : 1000
    });
    //动工日期
    this.dfConstructionStartDate = new Ext.form.DateField({
    	fieldLabel :'动工日期:',
    	//name : 'ConstructionStartDate',
    	labelSeparator: '',
		width: 120,
		format: 'Y-m-d'
	});
	
	 //竣工日期
    this.dfConstructionFinishDate = new Ext.form.DateField({
    	fieldLabel :'竣工日期:',
    	//name : 'ConstructionFinishDate',
    	labelSeparator: '',
		width: 120,
		format: 'Y-m-d'
	});
	//开发商id
	this.txtDevelopCompanyId = new Ext.form.Hidden({});
    //开发商
	this.txtDevelopCompanyName = new Ext.form.TextField({
		fieldLabel : '开发商',
		name :'DevelopCompany',
		width : 120,
		maxLength : 25
	});
	//开发商联系人
	this.txtDevelopContactId = new Ext.form.Hidden({
		name :'dcContactInfo.contactId',
		maxLength : 15	
	});
    this.txtDevelopContactName = new Ext.form.TextField({
		fieldLabel : '开发商联系人',
		name : 'dcContactInfo.userName',
		width : 120,
		maxLength : 25
	});
	
	//开发商联系电话
    this.txtDevelopContactPhone = new Ext.form.NumberField({
		fieldLabel : '开发商联系电话',
		name : 'dcContactInfo.phoneNumOffice',
		width : 120,
		maxLength : 15
	});
	//客户经理ID
	this.txtUnitManagerUserId = new Ext.form .Hidden({
    	name : 'userId'
    });
	//客户经理
    this.txtUnitManager = new Ext.form.TextField({
		fieldLabel: '客户经理',
		readOnly: true,
		name : 'userName',
		width: 120,
		listeners: {
		'render': function(cmp) {
				var self = cmp.ownerCt.ownerCt.ownerCt.ownerCt.ownerCt;
				//console.info(self)
				cmp.getEl().on('click', self.showSelector, self); 
			}
		}
	});
	//联系方式
	 this.txtUnitManagerPhone = new Ext.form.NumberField({
    	fieldLabel : '联系方式',
    	name : 'userPhone',
		width : 120,
		maxLength : 15
    });
    //行政区县
	this.cmbProvince = new Ext.form.ComboBox({
		width : 120,
        fieldLabel : '行政区县',
//      hiddenName : 'addressInfo.province',
        store: ['浦东新区'],
        editable: false,
        typeAhead:true,
        mode: 'local',
        triggerAction: 'all',
        selectOnFocus: true	
	});
	//街道路段
	this.txtAddressInfoNeighbourhood = new Ext.form.TextField({
		fieldLabel : '街道路段',
		name : 'neighbourhood',
		width : 120,
		maxLength : 10
	});
	//地址-路
	this.txtAddressId = new Ext.form.Hidden({
		name : 'buildingAddressId'
	});
	this.txtAddressInfoStreet = new Ext.form.TextField({
		fieldLabel : '地址 路',
		name :'street',
		width : 120,
		maxLength : 10
	});
	//弄
	this.txtAddressInfoAlley = new Ext.form.NumberField({
		fieldLabel: '弄',
		name :'alley',
		width: 60,
		maxLength : 10
	});
	//号
	this.txtAddressInfoDoorNum = new Ext.form.NumberField({
		fieldLabel: '号',
		name :'doorNum',
		width: 60,
		maxLength : 10
	});
	//基本信息
	this.form = new Ext.form.FormPanel({
		bodyStyle : 'padding: 5px 5px 0',
		frame :true,
		//fileUpload: true, 
		autoScroll : true,
		//autoHeight : true,
		height: 390,
		width : 750,
		labelAlign: 'right',
		labelWidth : 120,
		layout : 'table',
		layoutConfig : {columns:2},
		items:[
			{colspan:2,layout:'form',items:[this.txtBlankName]},
			{layout:'form', items:[this.txtDevelopCompanyName]},
			{layout:'form', items:[this.txtUnitManager]},
			{layout:'form', items:[this.txtDevelopContactName]},
			{layout:'form', items:[this.txtDevelopContactPhone]},
			{colspan:2,layout:'form',items:[this.cmbDevelopDesc]},
			{layout:'form', items:[this.dfConstructionStartDate]},			
            {layout:'form',items:[this.dfConstructionFinishDate]},
            //{layout:'form', items:[this.txtUnitManagerPhone]},
            {rowspan:3,layout:'form',items:[
            			{layout:'form',items:this.txtAddressInfoStreet},
            			{layout:'form',items:this.txtAddressInfoAlley},
            			{layout:'form',items:this.txtAddressInfoDoorNum}
            			]},
            {layout:'form', items:[this.cmbProvince]},
            {layout:'form', items:[this.txtAddressInfoNeighbourhood]},
			{layout:'form', items:[this.txtGridId]},
			{layout:'form', items:[this.txtBlankId]}
		]
	});
	
	//GIS信息
	this.txtGisId = new Ext.form.Hidden({
		name: 'gisInfo.id'
	});
	
	this.txtGisType = new Ext.form.Hidden({
		name: 'gisInfo.type',
		value: 1
	});
	//所属网格
	this.cmbGrid= new Ext.form.ComboBox({
		width :120,
		listWidth:135,
        fieldLabel: '所属网格',
        name : 'gridId',
        store : new Ext.data.JsonStore({
		   url:"../gridareas/listGrid.form",
		   autoLoad : true,
		   root:"grids",
		   fields:["gridId","gridName"]
		}),
		valueField : 'gridId',
		editable: false,
		displayField :'gridName',
        mode: 'local',
        //typeAhead:true,
       emptyText:'请选择所属网格',
        triggerAction: 'all',
        selectOnFocus: true
	});
	//所属区域
	this.cmbRegion = new Ext.form.ComboBox({
		width :120,
		listWidth:135,
		allowBlank : true,
		blankText:'请选择所属区域',
		fieldLabel: '所属区域',
		store : new Ext.data.JsonStore({
		   url:'../gridareas/listAllRegions.form',
		   autoLoad : true,
		   root:"regions",
		   fields:["areaId","name"]
		}),
		valueField : 'areaId',
		displayField :'name',
		editable: false,
        mode: 'local',
		triggerAction :'all',
		emptyText :'请选择所在区域',
		selectOnFocus :true	
	});
	 //坐标
    this.txtCoordsInfo = new Ext.form.TextArea({
		fieldLabel : '空地坐标',
		preventScrollbars : true,
		name :'coordsInfo',
		width : 400,
		heigth : 350
    });
    this.txtGroupCustInfoID = new Ext.form.Hidden({
    	name:'marketIdInfo'
    });
    //空地边框颜色
    this.txtLineColor = new Ext.form.ComboBox({
    	width :120,
		listWidth:135,
        fieldLabel: '空地边框颜色',
        name : 'LineColor',
        store: new Ext.data.SimpleStore({
        	fields: ['value', 'name'],
        	data: OMS.GIS_COLOR
        }),
        editable: false,
        valueField: 'value',
        displayField: 'name',
        typeAhead:true,
        mode: 'local',
        emptyText:'---请选择---',
        triggerAction: 'all',
        selectOnFocus: true
    });
    
    //空地填充颜色
    this.txtMainFillColor = new Ext.form.ComboBox({
    	width :120,
		listWidth:135,
        fieldLabel: '空地填充颜色',
        name : 'MainFillColor',
        store: new Ext.data.SimpleStore({
        	fields: ['value', 'name'],
        	data: OMS.GIS_COLOR
        }),
        valueField: 'value',
        editable: false,
        displayField: 'name',
        typeAhead:true,
        mode: 'local',
        emptyText:'---请选择---',
        triggerAction: 'all',
        selectOnFocus: true
    });
    //绘制空地
    this.btnDrawGis = new Ext.Button({
        text: '绘制空地',
        tooltip: {text: '绘制空地'},
        handler: this.viewGis,
        scope: this
    });
    
    //GIS信息--FormPanel
    this.gisform = new Ext.form.FormPanel({
		bodyStyle : 'padding: 5px 5px 0',
		frame :true,
		//autoHeight : true,
		id:'gisForm',
//		disabled:true,
		height:420,
		width : 700,
		labelAlign: 'right',
		labelWidth : 120,
		layout : 'table',
		layoutConfig : {columns:2},
		items:[
		    {layout:'form',items:[this.cmbRegion]},
			{layout:'form',items:[this.cmbGrid]},
			{layout:'form', items:[this.txtLineColor]},
			{layout:'form', items:[this.txtMainFillColor]},
			{colspan:2,layout:'form', items:[this.txtCoordsInfo]},
			{layout:'form', items:[this.txtGisType]},
            {colspan:2,layout:'form', items:[this.btnDrawGis]},
            {layout:'form', items:[this.txtGisId]}
		]
	});
	 this.tabs = new Ext.TabPanel({
        width:700,
        activeTab: 0,
        frame:true,
        //autoHeight:true,
        height : 420,
        deferredRender:false, 
		layoutOnTabChange:true,
        items:[
            {items:this.form, title: '基本信息'},
            {items:this.gisform, title: 'Gis信息'}
        ]
    });
    
	GridBlankInfo.Detail.superclass.constructor.call(this,{
		title : '基地空地信息',
		bodyStyle: 'padding: 5px 5px 0',
        autoHeight: true,
        width: 750,
        resizable: false,
        plain: true,
        modal: true,
        layout:'form',
        autoScroll: true,
        closeAction: 'hide',
        items :[this.tabs],
        buttons: [{
            text: '保存',
            cls: 'x-btn-text-icon btn-save',
            handler: this.onSave,
            scope: this
        }, {
            text: '关闭',
            cls: 'x-btn-text-icon btn-cancel',
            handler: this.hide.createDelegate(this, [])
        }]
		
	});
};

Ext.extend(GridBlankInfo.Detail,Ext.Window,{
		initAdd :function(){
			this.form.getForm().reset();
			this.gisform.getForm().reset();
		},
		initEdit : function(data){
			this.form.getForm().reset();
			this.gisform.getForm().reset();
			Ext.Ajax.request({
				url: '../gridblankinfo/prepare.form?timesnap=' + Date.parse(new Date()),
				method: 'POST',
				scope:this,
				params: {
					blank_id : data.BLANK_ID,
					unit_manager : data.UNIT_MANAGER,
					gis_id : data.GIS_ID,
					address_id : data.ADDRESS_ID,
					unit_id : data.UNIT_ID,
					develop_company : data.DEVELOP_COMPANY
					//entity_id : data.ENTITY_ID
					//entity_type :data.ENTITY_TYPE,
					//contact_type : data.CONTACT_TYPE
				},
				success:function(resp){	
					var resp = Ext.util.JSON.decode(resp.responseText);
					//基本信息
					if(resp.blank){
						this.txtBlankId.setValue(resp.blank[0].BLANK_ID);
						this.txtUnitId.setValue(resp.blank[0].UNIT_ID);			
						
						this.txtBlankName.setValue(resp.blank[0].BLANK_NAME);
						this.cmbDevelopDesc.setValue(resp.blank[0].DEVELOP_DESC);
						this.dfConstructionStartDate.setValue(resp.blank[0].CONSTRUCTION_START_DATE);
						this.dfConstructionFinishDate.setValue(resp.blank[0].CONSTRUCTION_FINISH_DATE);
					}
					if(resp.develop_company[0]){
						this.txtDevelopCompanyId.setValue(resp.develop_company[0].CUST_ID);
						this.txtDevelopCompanyName.setValue(resp.develop_company[0].CUST_NAME);
					}
					if(resp.contact[0]){
						this.txtDevelopContactId.setValue(resp.contact[0].CONTACT_ID);
						this.txtDevelopContactName.setValue(resp.contact[0].USER_NAME);
						this.txtDevelopContactPhone.setValue(resp.contact[0].PHONE_NUM_OFFICE);
					}
					if(resp.unit_manager[0]){
						this.txtUnitManagerUserId.setValue(resp.unit_manager[0].USER_ID);
						this.txtUnitManager.setValue(resp.unit_manager[0].USER_NAME);
						this.txtUnitManagerPhone.setValue(resp.unit_manager[0].USER_PHONE);
					}
					if(resp.unit){
						this.txtGridId.setValue(resp.unit[0].GRID_ID);	
						this.txtAddressId.setValue(resp.unit[0].ADDRESS_ID);
						this.cmbRegion.setValue(resp.unit[0].AREA_ID);
						this.cmbGrid.setValue(resp.unit[0].GRID_ID);
					}
					if(resp.address[0]){	
						this.txtAddressInfoStreet.setValue(resp.address[0].STREET);
						this.txtAddressInfoAlley.setValue(resp.address[0].ALLEY);
						this.txtAddressInfoDoorNum.setValue(resp.address[0].DOOR_NUM);
						this.cmbProvince.setValue(resp.address[0].DISTRICT);
						this.txtAddressInfoNeighbourhood.setValue(resp.address[0].NEIGHBOURHOOD);
					}
					//gis信息
					/*this.cmbDistrict.setValue(resp.blank[0].GRID);
					this.txtAddressInfoLongitude.setValue(resp.address[0].LONGITUDE);
					this.txtAddressInfoLatitude.setValue(resp.address[0].LATITUDE);
					this.txtGisIconId.setValue(resp.gis[0].ICON_ID);*/
					if(resp.gis[0]){
						this.txtGisId.setValue(resp.gis[0].GIS_ID);
						this.txtCoordsInfo.setValue(resp.gis[0].COORDS_INFO);
						this.txtLineColor.setValue(resp.gis[0].LINE_COLOR);
						this.txtMainFillColor.setValue(resp.gis[0].MAIN_FILL_COLOR);
						this.txtGisType.setValue(resp.gis[0].TYPE);
					}
				},
				failure:function(resp){	
					Utils.info('操作失败!','系统提示');
					return;
				}
			});
		},
		onSave:function(){
			if(!this.form.getForm().isValid()){
				Utils.info("信息填写不完整或有误,请检查!","系统提示");
				return;
			}
			this.conn =new Ext.data.Connection({
		        method: 'POST',
		        url: '../gridblankinfo/save.form?timesnap=' + Date.parse(new Date())
		    });
			Utils.request(this.conn,{
				scope: this,
				params: {
					//基本信息
					blank_id:this.txtBlankId.getValue(),
					unit_id :this.txtUnitId.getValue(),
					grid_id:this.txtGridId.getValue(),
					blank_name :this.txtBlankName.getValue(),
					develop_desc :this.cmbDevelopDesc.getValue(),
					
					develop_company:this.txtDevelopCompanyId.getValue(),
					cust_id:this.txtDevelopCompanyId.getValue(),
					cust_name :this.txtDevelopCompanyName.getValue(),
					contact_id:this.txtDevelopContactId.getValue(),
					contact_name:this.txtDevelopContactName.getValue(),
					contact_phone:this.txtDevelopContactPhone.getValue(),
					
					construction_start_date :this.dfConstructionStartDate.getValue()?this.dfConstructionStartDate.getValue().format('Y-m-d'):'',
					construction_finish_date :this.dfConstructionFinishDate.getValue()?this.dfConstructionFinishDate.getValue().format('Y-m-d'):'',
					
					unit_manager:this.txtUnitManagerUserId.getValue(),
					unit_manager_user_id:this.txtUnitManagerUserId.getValue(),
					unit_manager_user_name:this.txtUnitManager.getValue(),
					unit_manager_user_phone:this.txtUnitManagerPhone.getValue(),
					
					address_id:this.txtAddressId.getValue(),
					
					street:this.txtAddressInfoStreet.getValue(),
					alley:this.txtAddressInfoAlley.getValue(),
					door_num:this.txtAddressInfoDoorNum.getValue(),
					district:this.cmbProvince.getValue(),
					neighbourhood:this.txtAddressInfoNeighbourhood.getValue(),
					//gis信息
					/*grid_id:this.cmbDistrict.getValue(),
					longitude:this.txtAddressInfoLongitude.getValue(),
					latitude:this.txtAddressInfoLatitude.getValue(),
					icon_id:this.txtGisIconId.getValue(),
					gis_id:this.txtGisId.getValue(),*/
					grid_id:this.cmbGrid.getValue(),
					area_id:this.cmbRegion.getValue(),
					gis_id:this.txtGisId.getValue(),
					coords_info : this.txtCoordsInfo.getValue(),
					line_color : this.txtLineColor.getValue(),
					main_fill_color : this.txtMainFillColor.getValue(),
					groupCustInfoID:this.txtGroupCustInfoID.getValue(),
					type : this.txtGisType.getValue()
				},
				success:function(resp){	
					this.scope.hide();
					GridBlankInfo.List.instance.store.reload();
				}
			});
		},
		showSelector: function(event) {
			if (!this.userSelector) {
				this.userSelector = new Selector.User(this, '客户经理');
			}
			//console.info(this.users);
			this.userSelector.initUsers(this.users);
			this.userSelector.show();
		},
		
		setUsers: function(o) {	// for Selector.User callback
			if(o.length>0){
				this.users = o;
				this.txtUnitManager.setValue(this.users[0].name);
				this.txtUnitManagerUserId.setValue(this.users[0].id);
				this.selectUserId = this.users[0].id;
			}
		},
		initGridGis:function(lineColor,mainFillColor,gridName,coordInfo,groupCustInfoID,gridId){
			this.txtLineColor.setValue(lineColor);
			this.txtMainFillColor.setValue(mainFillColor);
			this.txtBlankName.setValue(gridName);
			this.txtCoordsInfo.setValue(coordInfo);
			this.txtGroupCustInfoID.setValue(groupCustInfoID);//社区当中所包含的集团客户ID集
			this.txtGridId.setValue(gridId);
		},
		viewGis:function(){
			var unitId = this.txtUnitId.getValue();
			var areaId = this.cmbRegion.getValue();
			var layerType;
			if(unitId==null||unitId==""){
				layerType = 1;
				if(areaId==''){
					Utils.warn('请先选择区域！');
					return;
				}
			}else{
				layerType = 2;
			}

			
			var str = OMS.GIS_IP+'/gis_for_management.jsp?areaId=' + areaId + '&lineColor=' + this.txtLineColor.getValue() + 
			'&mainFillColor=' + this.txtMainFillColor.getValue() + 
			'&name='+this.txtBlankName.getValue()+
			'&type=1'+
			'&did='+this.txtUnitId.getValue()+ 
			'&coordx='+this.txtCoordsInfo.getValue()+ '&layerType='+layerType;
			window.open(encodeURI(str), '_blank', 'toolbar=no,location=no,directories=no,scrollbars=no,menubar=no,width=1000,height=600,resizable=no,left=0,top=0, status=no');	
		}
		
});

//空地GIS 回调函数
 function initGridGis(lineColor,mainFillColor,gridName,coordInfo,groupCustInfoID,gridId){
	GridBlankInfo.Detail.instance.initGridGis(lineColor,mainFillColor,gridName,coordInfo,groupCustInfoID,gridId);
};